import 'package:flutter/material.dart';
import 'package:flutter_demo/pages/SplashPage.dart';
import 'package:flutter_demo/utils/TokenManager.dart';
import 'package:flutter_demo/pages/login/LoginPage.dart';
import 'package:flutter_demo/pages/home/HomePage.dart';
import 'package:flutter_demo/pages/mine/SettingPage.dart';

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => SplashPage(),
        '/login': (context) => LoginPage(),
        '/homePage': (context) => HomePage(),
        '/settings': (context) => SettingPage(),
      },
      onGenerateRoute: (settings) {
        // 获取token 有token去首页，没有去登录页
        bool isLogin = TokenManager().getToken().isNotEmpty;
        WidgetBuilder builder;
        if (isLogin) {
          switch (settings.name) {
            case '/':
              builder = (context) => SplashPage();
              break;
            case '/login':
              builder = (context) => LoginPage();
              break;
            case '/homePage':
              builder = (context) => HomePage();
              break;
            case '/settings':
              builder = (context) => SettingPage();
              break;
            default:
              builder = (context) => HomePage();
              break;
          }
        } else {
          builder = (context) => LoginPage();
        }
        return;
      },
    );
  }
}
